package com.zbl.zblproject.erp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.erp.entity.ErpGoods;
import com.zbl.zblproject.erp.entity.ErpHistoryGoodsPrice;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/8
 */
@Repository
public interface ErpGoodMapper extends BaseMapper<ErpGoods> {

    /**
     * 分页功能
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpGoods> selectPageBySearch(Page page, @Param("search") String search, @Param("status") int status);


    IPage<ErpGoods> selectPageBySearchAndCode(Page page, @Param("search") String search, @Param("code") int code, @Param("status") int status);


    /**
     * 分页功能
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpGoods> selectPageByCate(Page page, @Param("search") String search, @Param("cate") List<Integer> cate, @Param("status") int status);

    IPage<ErpGoods> selectPageByCateAndCode(Page page, @Param("search") String search, @Param("cate") List<Integer> cate, @Param("code") int code, @Param("status") int status);

    IPage<ErpGoods> selectPageForOut(IPage page,
                                     @Param("catId") Integer catId,
                                     @Param("brandId") Integer brandId,
                                     @Param("supplierId") Integer supplierId,
                                     @Param("goodsName") String goodsName,
                                     @Param("type") Integer type);

    String selectCode();
    /**
     * 分页功能查询仓库
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpGoods> selectWareItemPage(Page page, @Param("search") String search);


    /**
     * 分页功能
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpGoods> selectPageBySearchAndSup(Page page, @Param("search") String search, @Param("sup") int sup, @Param("status") int status);


    IPage<ErpGoods> selectPageBySearchAndSupAndCode(Page page, @Param("search") String search, @Param("sup") int sup, @Param("code") int code);



    /**
     * 分页功能
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpGoods> selectPageByCateAndSup(Page page, @Param("search") String search, @Param("cate") List<Integer> cate, @Param("sup") int sup, @Param("status") int status);

    IPage<ErpGoods> selectPageByCateAndSupAndCode(Page page, @Param("search") String search, @Param("cate") List<Integer> cate, @Param("sup") int sup, @Param("code") int code, @Param("sale") int sale, @Param("status") int status, @Param("brand") int brand, @Param("codesn") String codesn);



    ErpGoods selectByGoodsSn(String goodsSn);



    /**
     * 查询入库存
     * @param goodId
     * @return
     */
    Integer useStockByIn(int goodId);

    /**
     * 查询出库存
     * @param goodId
     * @return
     */
    Integer useStockByOut(int goodId);

    @Select("SELECT goods_number FROM zbl_goods WHERE goods_id = #{id}")
    Integer selectGoodsNumberById(@Param("id") int goodsId);

    @Select("SELECT suppliers_id FROM zbl_goods WHERE goods_id = #{id}")
    Integer selectSupplierById(@Param("id") int goodsId);

    /**
     * 更新商品库存
     * @param goodsId
     * @param number
     * @return
     */
    Integer updateStockByGoodsId(@Param("id") int goodsId, @Param("number") int number);

    /**
     * 更新商品供应商
     * @param goodsId
     * @param suppliersid
     * @return
     */

    Integer updateSuppliersByGoodsId(@Param("goodsId") int goodsId, @Param("suppliersid") int suppliersid);

    //更新价格
    Integer updateSuppliersJG(@Param("shopprice") int shopprice, @Param("goodsId") int goodsId);





    List<Integer> selectStockOutGoods();

    /**
     * 根据goodsId获取品牌名称
     * @param goodsId 商品ID
     * @return 品牌名称
     */
    String getBrandNameByGoodsId(int goodsId);

    String getGoodsNameByGoodsId(int goodsId);

    @Select("SELECT COUNT(*) FROM zbl_goods WHERE goods_sn = #{goodsSn}")
    int checkGoodsSnIsExist(@Param("goodsSn") String goodsSn);

    @Select("SELECT * FROM zbl_goods WHERE goods_number > 0")
    List<ErpGoods> selectListHasStock();

    @Select("SELECT * FROM zbl_goods WHERE brand_id = #{brandId}")
    List<ErpGoods> selectGoodsByBrandId(@Param("brandId") int brandId);

    @Select("SELECT status FROM zbl_goods WHERE goods_id = #{goodsId}")
    int checkGoodsCanUse(@Param("goodsId") int goodsId);

    @Update("UPDATE zbl_goods SET goods_number = goods_number - #{goodsNum} WHERE goods_id = #{goodsId} ")
    boolean subGoodsNum(@Param("goodsId") int goodsId, @Param("goodsNum") int goodsNum);

    @Select("SELECT goods_id FROM zbl_goods WHERE goods_sn = #{goodsSn}")
    Integer selectGoodsId(@Param("goodsSn") String goodsSn);

    @Select("SELECT commodity_code FROM zbl_goods WHERE commodity_code LIKE '${flow}%' ORDER BY commodity_code DESC LIMIT 1")
    String selectMaxFlowNumber(@Param("flow") String flowNum);

    @Select("SELECT market_price FROM zbl_goods WHERE goods_id = #{id}")
    BigDecimal getMarketPriceByGoodsId(@Param("id") Integer id);

    @Select("SELECT shop_price FROM zbl_goods WHERE goods_id = #{id}")
    BigDecimal getshoPriceByGoodsId(@Param("id") Integer id);


    @Select("SELECT IFNULL(SUM(a.goods_num), 0) FROM zbl_pay_orderitem AS a LEFT JOIN zbl_pay_order AS b ON a.order_id = b.id WHERE b.STATUS = 1 AND DATE_SUB(CURDATE(), INTERVAL #{orderCycle} DAY) <= b.create_time AND a.goods_id = #{goodsId} GROUP BY a.goods_id")
    Integer selectAmontOfThisMonth(@Param("goodsId") Integer goodsId, @Param("orderCycle") Integer orderCycle);

    @Select("SELECT IFNULL(SUM(a.goods_num), 0) FROM zbl_pay_orderitem AS a LEFT JOIN zbl_pay_order AS b ON a.order_id = b.id WHERE b. STATUS = 1 AND a.goods_id = #{goodsId}")
    Integer selectAmontOfAll(@Param("goodsId") Integer goodsId);

    List<ErpGoods> selectOutGoods();

    Integer getCycleGoodsNumber(int goodsId);

    @Update("UPDATE zbl_goods SET pre_status = 0 WHERE pre_status = #{preStatus}")
    boolean updatePreStatusToZero(Integer preStatus);

    @Select("SELECT goods_sn FROM zbl_goods WHERE goods_id = #{id} ")
    String selectGoodsSn(Integer id);

//    @Select("select promoteStartDate,promoteEndDate,orderSn,promotePrice from sheet1")
//    List<ErpTemporaryGoods> selectAllSheet();
//
//    int updateSheet(@Param("promoteStartDate") long promoteStartDate,
//                    @Param("promoteEndDate") long promoteEndDate,
//                    @Param("orderSn") String orderSn,
//                    @Param("promotePrice") BigDecimal promotePrice);


    ErpGoods selectByGoodsSns(@Param("goodsSn") String goodsSn);

    int updateGoodsPrice(ErpGoods erpGoods);


}
